# get 查询参数: 既然是参数，就有必填、默认值、可选，提取前端传值时，会自动按标注的类型进行转换

from fastapi import FastAPI

app = FastAPI()


# 单路径
@app.get("/items/{item_id}")
async def read_user_item(
        item_id: str,  # 必填且类型是 str
        needy: str,
        skip: int = 0,  # 可选，不传默认0，传则必须 int 类型
        limit: int | None = None  # 可选，不传默认 None，返回的是 null
):
    item = {
        "item_id": item_id,
        "needy": needy,
        "skip": skip,
        "limit": limit
    }
    return item


# 多路径
@app.get("/users/{user_id}/items/{item_id}")
async def read_user_item(
        user_id: int,
        item_id: str,
        q: str | None = None,
        short: bool = False #
):
    item = {"item_id": item_id, "owner_id": user_id}
    if q:
        item.update({"q": q})
    if not short:
        item.update(
            {"description": "This is an amazing item that has a long description"}
        )
    return item
